package xw4g08.sameAs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.http.HTTPRepository;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.Rio;
import xw4g08.Configuration;

public class SameAsGenerator {

	public static void main(String[] args) {
		
		LocalCon();
		
	}
	
	private static void RemoteCon() {
		HTTPRepository rep = null;
		RepositoryConnection con = null;
		GeneratorCore gc = new GeneratorCore();
		Configuration.init();
		List<String> urls = Configuration.REP_URLs;
		for(int i = 0;i < Configuration.REP_NUM;i++){
			String url = urls.get(i);
			System.out.println("Processing " + url);
			rep = new HTTPRepository(url);
			try {
				rep.initialize();
				con = rep.getConnection();
				String[] temp = url.split("/");
				gc.sameasWriter(con, temp[temp.length-1]);
			} catch (RepositoryException e) {
				e.printStackTrace();
			}
		}
	}
		
		private static void LocalCon() {
			File input = Configuration.IN_DIR;
			File[] files = input.listFiles();
			RDFFormat format = Rio.getParserFormatForFileName(files[0].getName());
			RDFParser parser = Rio.createParser(format);
			for(File f:files) {
				SAGenHandler sagenerater = new SAGenHandler("sa-"+f.getName());
				sagenerater.SetFormat(format);
				parser.setRDFHandler(sagenerater);
				FileInputStream in;
				try {
					in = new FileInputStream(f);
					parser.parse(in, "");
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (RDFParseException e) {
					e.printStackTrace();
				} catch (RDFHandlerException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
			
		}
		
}
	

